package com.ace.slide.service.impl;

import java.util.Calendar;

import org.hibernate.Session;

import com.ace.slide.client.service.SlideShowBuilderService;
import com.ace.slide.data.BaseDAO;
import com.ace.slide.data.MemberDAO;
import com.ace.slide.data.ProfileDAO;
import com.ace.slide.model.Member;
import com.ace.slide.model.Profile;
import com.ace.slide.service.ShowBuilderContextService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ShowBuilderServiceImpl extends RemoteServiceServlet implements
        SlideShowBuilderService
{

    private ShowBuilderContextService context;

    private static final long serialVersionUID = 1L;

    public ShowBuilderServiceImpl()
    {
        context = new ShowBuilderContextServiceImpl();
        try
        {
            context.wireServices();
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    public String processLogin(String username, String password)
    {
        Session session = context.getHibernateService().createSession();
        String result = "";
        try
        {
            BaseDAO.setSession(session);

            session.beginTransaction();
            Member member;
            Calendar cal = Calendar.getInstance();

            member = MemberDAO.authenticate(username, password);

            if (member != null)
            {
            	member.getProfile().setLastLogin(cal.getTime());
            	ProfileDAO.save(member.getProfile());
            	if(member.getFirstName().equals(""))  {
            		result = member.getUsername();
            	}
            	else result = member.getFirstName();
            }
            session.getTransaction().commit();
        }
        catch (Exception ex)
        {
            try
            {
                session.getTransaction().rollback();
            }
            catch (Exception crappy)
            {
            }
            ex.printStackTrace();
        }
        finally
        {
            session.close();
        }

        return result;
    }

	public String processRegister(String firstName, String lastName,
			String username, String password, String email) {
		
		Session session = context.getHibernateService().createSession();
		
		String result = "";
		Calendar cal = Calendar.getInstance();
		Member m = new Member();
        
		m.setUsername(username);
        m.setPassword(password);
        m.setFirstName(firstName);
        m.setLastName(lastName);
        m.setMember(true);
        
        try {
			BaseDAO.setSession(session);
			session.beginTransaction();
			if (MemberDAO.isUnique(m)) {

				Profile p = new Profile();
				p.setMemberSince(cal.getTime());
				p.setEmail(email);

				m.setProfile(p);
				MemberDAO.save(m);
				p.setMemberId(m.getMemberId());
				ProfileDAO.save(p);

			} else {
				result = "";
			}
			session.getTransaction().commit();
			result = processLogin(m.getUsername(), m.getPassword());
		} catch (Exception ex) {
			session.getTransaction().rollback();
			ex.printStackTrace();
		} finally {
			session.close();
		}
        
		return result;
	}

}
